package com.xingkeduo.controller;

import com.github.pagehelper.PageInfo;
import com.xingkeduo.model.param.AttendancePageParam;
import com.xingkeduo.model.param.AttendanceParam;
import com.xingkeduo.model.po.TStaffAttendance;
import com.xingkeduo.model.vo.*;
import com.xingkeduo.service.StaffAttendanceService;
import com.xingkeduo.service.StorePlanService;
import com.xingkeduo.service.impl.StaffAttendanceServiceImpl;
import com.xingkeduo.util.tool.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 中文输入法发挥不稳定的刘川
 * @Date: 19-3-18 下午3:18
 */
@RestController
@RequestMapping("staff/attendance")
public class StaffAttendanceController {

    private static Logger log = LoggerFactory.getLogger(StaffAttendanceController.class);

    @Autowired
    StaffAttendanceService attendanceService;

    @Autowired
    StorePlanService storePlanService;

    /**
     * @param attendanceList
     * @return com.xingkeduo.util.tool.ResponseMessage
     * @description 批量排班
     * @author 中文输入法发挥不稳定的刘川
     * @date 19-4-16 下午3:16
     */
    @PostMapping("/updateStaffWorkTime")
    public ResponseMessage updateStaffWorkTime(@RequestBody List<TStaffAttendance> attendanceList) {
        log.info("排班接口请求入参："+attendanceList.get(0).toString());
        for(TStaffAttendance staffAttendance : attendanceList){
            if(null == staffAttendance.getId()){
                return ResponseMessage.error("缺少排班Id", 500);
            }
            if(StringUtils.isEmpty(staffAttendance.getManagerId()) || StringUtils.isEmpty(staffAttendance.getManagerName())){
                return ResponseMessage.error("缺少ManagerId or 缺少排班人ManagerName", 500);
            }
        }
        return attendanceService.updateStaffWorkTime(attendanceList);
    }

    /**
     * @description  查找重新排班受影响的订单
     * @Author 情绪极其不稳定的Shadow
     * @Param [staffAttendance]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @PostMapping("/getImpactOrderByScheduling")
    public ResponseMessage getImpactOrderByScheduling(@RequestBody TStaffAttendance staffAttendance) {
        if(null == staffAttendance.getId()){
            return ResponseMessage.error("缺少排班Id", 500);
        }
        if(StringUtils.isEmpty(staffAttendance.getManagerId())){
            return ResponseMessage.error("缺少ManagerId", 500);
        }
        return attendanceService.getImpactOrderByScheduling(staffAttendance);
    }

    /**
     * @description  获取传入时间段排班情况
     * @Author 情绪极其不稳定的Shadow
     * @Param [selectedDate]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStaffAttendance")
    public ResponseMessage getStaffAttendance(String selectedDate, Long storeId){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺Id", 500);
        }
        Map<Long, StaffAttendanceSummaryVo> voMap = attendanceService.getStaffAttendance(selectedDate, storeId, null);
        return ResponseMessage.ok(voMap.values());
    }

    /**
     * @description  获取发型师当天排班
     * @Author 情绪极其不稳定的Shadow
     * @Param [staffId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStaffAttendanceByStaffId")
    public ResponseMessage getStaffAttendanceByStaffId(Long staffId, String date){
        if(null == staffId){
            return ResponseMessage.error("发型师ID不能为空",500);
        }
        List<StaffAttendanceVo> staffAttendanceByStaffId = attendanceService.getStaffAttendanceByStaffId(staffId, date);
        return ResponseMessage.ok(staffAttendanceByStaffId);
    }

    /**
     * @description  考勤列表（店铺）
     * @Author 情绪极其不稳定的Shadow
     * @Param [storeId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStaffAttendanceByStoreId")
    public ResponseMessage getStaffAttendanceByStoreId(Long storeId){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺id", 500);
        }
        StoreAttendanceVo storeAttendanceVo = attendanceService.getStaffAttendanceByStoreId(storeId);
        return ResponseMessage.ok(storeAttendanceVo);
    }

    /**
     * @description   考勤列表（发型师）
     * @Author 情绪极其不稳定的Shadow
     * @Param [param]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @PostMapping("/getAttendanceOfPersonal")
    public ResponseMessage getAttendanceOfPersonal(@RequestBody AttendanceParam param){
        if(null == param.getStaffId()){
          return ResponseMessage.error("发型师ID不能为空",500);
        }
        List<StaffAttendanceVo> attendanceVos = attendanceService.getAttendanceOfPersonal(param);
        if(null == attendanceVos || attendanceVos.size() == 0){
            return ResponseMessage.ok(new ArrayList<>());
        }
        return ResponseMessage.ok(attendanceVos);
    }

    /**
     * @description  获取发型师历史打卡店铺
     * @Author 情绪极其不稳定的Shadow
     * @Param [staffId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getAttendanceStoresByStaffId")
    public ResponseMessage getAttendanceStoresByStaffId(Long staffId){
        if(StringUtils.isEmpty(staffId)){
            return ResponseMessage.error("缺少发型师Id", 500);
        }
        List<StoreInfoVo> vos = attendanceService.getAttendanceStoresByStaffId(staffId);
        return ResponseMessage.ok(vos);
    }

    /**
     * 我就测试一下，别搞事情
     * 分页Page
     * @return
     */
    @GetMapping("/getAttendanceList")
    public ResponseMessage getAttendanceList(AttendancePageParam attendancePageParam){
        PageInfo<CmsStaffAttendancePageVo> attendanceList = attendanceService.getAttendanceList(attendancePageParam);
        return ResponseMessage.ok(attendanceList);
    }

    /**
     * 查看排版详情
     * @param attendanceId
     * @return
     */
    @GetMapping("/getAttendanceInfo")
    public ResponseMessage getAttendanceInfo(Integer attendanceId){
        if(StringUtils.isEmpty(attendanceId)){
            return ResponseMessage.error("库存ID不存在", 500);
        }
        CmsStaffAttendanceVo attendanceInfo = attendanceService.getAttendanceInfo(attendanceId);
        return ResponseMessage.ok(attendanceInfo);
    }

}
